package com.abe.mvp.view.activity;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

import com.abe.config.CardReaderUtils;
import com.abe.config.CardReaderUtils.CardReadBean;
import com.abe.config.SerialPortConstants;
import com.abe.mvp.contact.SerialPortContact;
import com.abe.mvp.model.bean.SerialPortDataBean;
import com.abe.mvp.presenter.SerialPortPresenterImp;
import com.abe.mvp.view.activity.base.BaseActivity;
import com.abe.threadmill.R;
import com.abe.utils.ConvertUtils;
import com.abe.utils.NormalUtils;

@SuppressLint("SimpleDateFormat")
@ContentView(R.layout.activity_test_card_reader)
public class CardReaderActivity extends BaseActivity implements
		SerialPortContact.View {

	private static final int SHOW_CARD_NUM = 0x001;

	SerialPortContact.Presenter presenter;

	@ViewInject(R.id.radio_button_open)
	private RadioButton radio_button_open;
	@ViewInject(R.id.radio_button_close)
	private RadioButton radio_button_close;

	@ViewInject(R.id.button_open_sericalport)
	private Button button_open_sericalport;
	@ViewInject(R.id.button_read_card_num)
	private Button button_read_card_num;

	@ViewInject(R.id.edit_card_address)
	private EditText edit_card_address;
	@ViewInject(R.id.edit_card_num)
	private EditText edit_card_num;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		presenter = null;
	}

	private void initView() {
		presenter = new SerialPortPresenterImp(this);
	}

	@Event(value = R.id.button_open_sericalport, type = View.OnClickListener.class)
	private void openSericalPort(View view) {
		/** 打开串口 */
		presenter.openSerialPort(SerialPortConstants.SerialPort_ttys3);
	}

	@Event(value = R.id.button_read_card_num, type = View.OnClickListener.class)
	private void readCardNum(View view) {
		byte[] cmd = GetCommand();
		if (cmd != null) {
			/** 读卡号 */
			presenter.sendCommand(cmd);
		}
	}

	/** 获取读卡号命令 */
	public byte[] GetCommand() {
		/** 蜂鸣器 */
		RadioButton[] buttons = { radio_button_open, radio_button_close };
		/** 地址转换 */
		String address = edit_card_address.getText().toString().trim();
		if (address.equals("")) {
			showToast(R.string.card_address_empty);
			return null;
		}
		byte address_byte = ConvertUtils.hexString2Bytes(address)[0];

		/** 数据示例:01 08 a1 20 00 00 00 77 */
		byte[] Cmd_Read_Id = new byte[8];
		Cmd_Read_Id[0] = 0x01;
		Cmd_Read_Id[1] = 0x08;
		Cmd_Read_Id[2] = (byte) 0xa1;
		Cmd_Read_Id[3] = address_byte;
		Cmd_Read_Id[4] = 0x00;
		if (getRButton(buttons) == NormalUtils.getString(R.string.common_close)) {
			Cmd_Read_Id[5] = 0x00;
		} else if (getRButton(buttons) == NormalUtils
				.getString(R.string.common_open)) {
			Cmd_Read_Id[5] = 0x01;
		} else {
			showToast(R.string.card_buzzer_setting);
			return null;
		}
		Cmd_Read_Id[6] = 0x00;
		CardReaderUtils.CheckSum(Cmd_Read_Id, Cmd_Read_Id[1]);
		return Cmd_Read_Id;
	}

	@Override
	public void onConnectSuccess() {

	}

	@Override
	public void onSendDataSuccess() {

	}

	@Override
	public void onReceiveDataSuccess(SerialPortDataBean data) {
		/** 数据包解析 */
		CardReadBean bean = CardReaderUtils.Parser(data.getBuffer());
		localSendMessage(NormalUtils.getMessage(SHOW_CARD_NUM, bean.getInfo()));
	}

	@Override
	public void onCloseSuccess() {

	}

	@Override
	public void onError(String errorMsg) {

	}

	/** Handler快捷 */
	protected void localSendMessage(Message message) {
		new MyHandler(Looper.getMainLooper()).sendMessage(message);
	}

	class MyHandler extends Handler {
		public MyHandler(Looper looper) {
			super(looper);
		}

		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SHOW_CARD_NUM: {
				edit_card_num.setText(msg.obj.toString());
			}
				break;
			}
		}
	}

	public String getRButton(RadioButton temp[]) {
		String str = "";
		for (RadioButton btn : temp) {
			if (btn.isChecked()) {
				str = btn.getText().toString();
			}
		}
		return str;
	}

	public void setRButton(RadioButton temp[], String str) {
		for (RadioButton btn : temp) {
			if (str != null && str.equals(btn.getText().toString())) {
				btn.setChecked(true);
			}
		}
	}

}
